﻿using Castle.DynamicProxy;

using PLCS.App.Shared.Models;
using PLCS.Client.IService;
using PLCS.Client.IService.ConfigManager;

using System.Collections.ObjectModel;

using Volo.Abp.Application.Dtos;

namespace PLCS.App.Shared.ViewModels;

public class NavigationPagedCurdViewModel<Dto, EditDto, IService> : NavigationViewModel
       where IService : class, IBaseService<Dto, EditDto>
       where Dto : EntityDto<Guid>
       where EditDto : EntityDto<Guid>
{
    #region 翻页

    /// <summary>
    /// 总数
    /// </summary>
    private int _totalCount = 0;

    /// <summary>
    /// 每页大小
    /// </summary>
    private int _pageSize = GlobalVariable.PageSize;

    /// <summary>
    /// 当前页
    /// </summary>
    private int _pageIndex = 1;

    /// <summary>
    /// 分页总数
    /// </summary>
    private int _pageCount;

    public int TotalCount
    {
        get { return _totalCount; }
        set { _totalCount = value; RaisePropertyChanged(); }
    }

    public int PageSize
    {
        get { return _pageSize; }
        set { _pageSize = value; RaisePropertyChanged(); }
    }

    public int PageIndex
    {
        get { return _pageIndex; }
        set { _pageIndex = value; RaisePropertyChanged(); }
    }

    public IService _service;

    public int PageCount
    {
        get { return _pageCount; }
        set { _pageCount = value; RaisePropertyChanged(); }
    }

    /// <summary>
    /// 首页
    /// </summary>
    public virtual async void GoHomePage()
    {
        if (PageIndex == 1) return;

        PageIndex = 1;

        await GetPageData(PageIndex);
    }

    /// <summary>
    /// 上一页
    /// </summary>
    public virtual async void GoOnPage()
    {
        if (PageIndex == 1) return;

        PageIndex--;

        await GetPageData(PageIndex);
    }

    /// <summary>
    /// 下一页
    /// </summary>
    public virtual async void GoNextPage()
    {
        if (PageIndex == PageCount) return;

        PageIndex++;

        await GetPageData(PageIndex);
    }

    /// <summary>
    /// 尾页
    /// </summary>
    public virtual async void GoEndPage()
    {
        PageIndex = PageCount;

        await GetPageData(PageCount);
    }

    /// <summary>
    /// 获取数据
    /// </summary>
    /// <param name="pageIndex"></param>
    public virtual async Task GetPageData(int pageIndex)
    {
        await Task.CompletedTask;
    }

    public override async Task RefreshAsync()
    {
        await GetPageData(PageIndex = 1);
    }

    /// <summary>
    /// 设置页数
    /// </summary>
    public virtual void SetPageCount()
    {
        PageCount = (int)Math.Ceiling(TotalCount / (double)PageSize);
    }

    public DelegateCommand GoHomePageCommand { get; }

    public DelegateCommand GoOnPageCommand { get; }

    public DelegateCommand GoNextPageCommand { get; }

    public DelegateCommand GoEndPageCommand { get; }

    #endregion 翻页

    #region 增删改查

    private ObservableCollection<Dto> _dataGridList;

    public ObservableCollection<Dto> DataGridList
    {
        get { return _dataGridList; }
        set { SetProperty(ref _dataGridList, value); }
    }

    public DelegateCommand AddCommand { get; }
    public DelegateCommand<object> EditCommand { get; }
    public DelegateCommand QueryCommand { get; }
    public DelegateCommand<object> DeleteCommand { get; }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="model"></param>
    public virtual async void Delete(object model)
    {
        var res = ShowQuestion("确认删除吗？");
        if (!res)
            return;
        if (model != null)
        {
            var data = model as Dto;
            await _service.DeleteByIdAsync(data.Id);
            DataGridList.Remove(data);
            TotalCount--;
            ShowMessage("操作成功");
        }
    }

    /// <summary>
    /// 新增
    /// </summary>
    public async void Add()
    {
        var dialogResult = await dialog.ShowDialogAsync(GetPageName("Detail"));
        if (dialogResult.Result == ButtonResult.OK)
            await RefreshAsync();
    }

    /// <summary>
    /// 编辑
    /// </summary>
    public async void Edit(object model)
    {
        DialogParameters param = new DialogParameters
        {
            { "Value", model }
        };
        var dialogResult = await dialog.ShowDialogAsync(GetPageName("Detail"), param);
        if (dialogResult.Result == ButtonResult.OK)
            await RefreshAsync();
    }

    /// <summary>
    /// 获取弹出页名称
    /// </summary>
    /// <param name="methodName"></param>
    /// <returns></returns>
    private string GetPageName(string methodName) => GetType().Name.Replace("ViewModel", methodName + "View");

    #endregion 增删改查

    public override async Task OnNavigatedToAsync(NavigationContext navigationContext)
    {
        if (DataGridList == null || DataGridList.Count == 0)
            await RefreshAsync();
    }

    public NavigationPagedCurdViewModel(IService service)
    {
        //ProxyGenerator proxy = new ProxyGenerator();
        //LogShowInterceptor customInterceptor = new LogShowInterceptor();
        //_service = proxy.CreateInterfaceProxyWithTarget<IService>(service, customInterceptor);
        _service = service;

        QueryCommand = new DelegateCommand(async () => await RefreshAsync());
        GoHomePageCommand = new DelegateCommand(GoHomePage);
        GoOnPageCommand = new DelegateCommand(GoOnPage);
        GoNextPageCommand = new DelegateCommand(GoNextPage);
        GoEndPageCommand = new DelegateCommand(GoEndPage);
        AddCommand = new DelegateCommand(Add);
        EditCommand = new DelegateCommand<object>(Edit);
        DeleteCommand = new DelegateCommand<object>(Delete);
    }
}